package com.fancon.rage.comic;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import com.fancon.rage.comic.dao.PhotoDao;
import com.fancon.rage.comic.entity.Photo;

@SuppressWarnings("serial")
public class GaeRageComicVnServlet extends AbstractUploadServlet {
    private PhotoDao dao = new PhotoDao();    

    @Override
    protected void showForm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("datastore.jsp").forward(req, resp);        
    }

    @Override
    protected void handleSubmit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletFileUpload upload = new ServletFileUpload();

        try {
            FileItemIterator it = upload.getItemIterator(req);

            Photo photo = new Photo();

            while (it.hasNext()) {
                FileItemStream item = it.next();
                String fieldName = item.getFieldName();
                InputStream fieldValue = item.openStream();

                if ("title".equals(fieldName)) {
                    photo.setTitle(Streams.asString(fieldValue));
                    continue;
                }

                if ("caption".equals(fieldName)) {
                    photo.setCaption(Streams.asString(fieldValue));
                    continue;
                }

                if ("file".equals(fieldName)) {
                    photo.setContentType(item.getContentType());
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    Streams.copy(fieldValue, out, true);
                    photo.setPhotoData(out.toByteArray());
                    continue;
                }
            }

            dao.save(photo);
            resp.sendRedirect("datastore?action=display&id=" + photo.getId());            
        } catch (FileUploadException e) {
            throw new ServletException(e);
        }        
    }

    @Override
    protected void showRecord(long id, HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Photo photo = dao.findById(id);
        
        resp.setContentType(photo.getContentType());        
        resp.getOutputStream().write(photo.getPhotoData());
        resp.flushBuffer();                    
    }

	@Override
	protected void showThumb(long id, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void delete(long id, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}
}
